package thread;

import java.util.*;

public class SyncDemo4 {
    public static void main(String[] args) {
        //List<Integer> c = new ArrayList<>();//异步
        //List<Integer> c = new ArrayList<>();//异步
        //Set<Integer> c = new HashSet<>();//异步



        //List<Integer> c = Collections.synchronizedList(new ArrayList<>());//同步
        //List<Integer> c = Collections.synchronizedList(new LinkedList<>());//同步
        Set<Integer> c = Collections.synchronizedSet(new HashSet<>());//同步
        Thread t1 = new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++) {
                    c.add(i);
                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for (int i = 1000; i < 2000; i++) {
                    c.add(i);
                }
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        }
        System.out.println(c.size());

    }
}
